using AllIn1SpriteShader; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using wildlogicgames; namespace RebelHeartEclipse { public enum PooledObjID { ArrowObj = 0, BloodObj = 1, }; public class ObjPooler : MonoBehaviour { [Header("--------------------BATTLE GROUND GRIDGEN PLUGIN")] [Header("Plugin the grid generator here.")] [SerializeField] public BattleGroundGridGenerator _battleGroundGridGenerator; public static ObjPooler _instance; private Transform _transform; private Vector3 _vectorOffset; private static int _poolSizeArrowObj = 10; private static int _poolSizeBloodObj = 10; [Header("Prefabs To Pool")] [Tooltip("The order must corrispond to enum PooledObjID")] public GameObject[] _prefabGameObjects; public Dictionary> _pooledObjsDict; private void Awake() //=> Setup(); { if (_instance == null) { _instance = this; _transform = this.transform; Setup(); } else if (_instance != this) { Destroy(this.gameObject); } DontDestroyOnLoad(this.gameObject); } private void Setup() { if (_prefabGameObjects == null) return; for (int i = 0; i < _prefabGameObjects.Length; i++) { _prefabGameObjects[i].SetActive(false); } if (_pooledObjsDict == null) _pooledObjsDict = new Dictionary>(); _pooledObjsDict.Add(PooledObjID.ArrowObj, new List()); _pooledObjsDict.Add(PooledObjID.BloodObj, new List()); for (int i = 0; i < _poolSizeArrowObj; i++) AddGameObject(PooledObjID.ArrowObj); for (int i = 0; i < _poolSizeBloodObj; i++) AddGameObject(PooledObjID.BloodObj); } private void AddGameObject(PooledObjID pooledObjID) { //Reference to the on-disk asset. Don't do anything to that,Instead, use Instantiate() and use the copy. GameObject newGameObject = (GameObject)Instantiate(_prefabGameObjects[(int)pooledObjID]); newGameObject.transform.parent = _transform; if (pooledObjID == PooledObjID.ArrowObj) { newGameObject.GetComponent().Plugin(ref _battleGroundGridGenerator); } if (pooledObjID == PooledObjID.BloodObj) { newGameObject.GetComponent().Plugin(ref _battleGroundGridGenerator); } newGameObject.SetActive(false); if (_pooledObjsDict.TryGetValue(pooledObjID, out List listPlayer)) listPlayer.Add(newGameObject); } private GameObject GetPooledObject(PooledObjID pooledObjID) { if (_pooledObjsDict == null) return null; if (_pooledObjsDict.TryGetValue(pooledObjID, out List list)) { for (int i = 0; i < list.Count; i++) { GameObject obj = list[i]; if (obj != null && !obj.activeInHierarchy) { obj.SetActive(true); // Enable the object return obj; } } // No inactive object found, instantiate a new one //Debug.Log("Instantiating from obj pool!"); //GameObject newGameObject = Instantiate(_prefabGameObjects[(int)pooledObjID]); //list.Add(newGameObject); //return newGameObject; } return null; } public GameObject InstantiateObj(PooledObjID pooledObjID, Transform positionToSpawn, Quaternion q) { GameObject obj = GetPooledObject(pooledObjID); if (obj == null) return null; if (positionToSpawn == null) return null; _vectorOffset = positionToSpawn.position; Vector3 v = Vector3.zero; if (pooledObjID == PooledObjID.ArrowObj) { //obj.GetComponent().Plugin(ref _battleGroundGridGenerator); obj.GetComponent().Setup(); } if (pooledObjID == PooledObjID.BloodObj) { obj.GetComponent().Setup(); _vectorOffset.y += 0.33f; int rand = wildlogicgames.Utilities.GetRandomNumberInt(0, 100); if (rand < 33) _vectorOffset.x -= 0.05f; if (rand >= 33 && rand <= 66) _vectorOffset.x += 0.05f; if (rand > 66) _vectorOffset.y -= 0.05f; } _vectorOffset.z -= 0.01f; obj.transform.position = _vectorOffset + v; obj.transform.rotation = q; obj.transform.parent = _transform; obj.SetActive(true); return obj; } } }